
var LOTTERYLIST_DATA = {};

$(function() {
    getGameListState_main();
});

/**
 * 获取游戏子集
 * @param type 游戏种类
 */
function getGameListState_main(type) {
    var gameMenusDiv = $("#gameMenus");
    var gameMenuDiv = $("#gameMenu");
    gameMenusDiv.html('');
    if(gameMenusDiv.length < 1) {
        setTimeout(function(){
            getGameListState_main(type);
        }, 200);
        return;
    }
    $.ajax({
        type: "post",
        url: "/ct-data/loadLottery",
        data: {"type": type},
        async: false,
        timeout:10000,
        datatype: "json",
        success: function(data) {
            if(data.sign === true) {
                // 置空容
                LOTTERYLIST_DATA.l_g_ssc = [];
                LOTTERYLIST_DATA.l_g_11x5 = [];
                LOTTERYLIST_DATA.l_g_k3 = [];
                LOTTERYLIST_DATA.l_g_xy3d = [];
                LOTTERYLIST_DATA.l_g_keno = [];
                LOTTERYLIST_DATA.l_g_lucky = [];
                LOTTERYLIST_DATA.l_ssc = [];
                LOTTERYLIST_DATA.l_11x5 = [];
                LOTTERYLIST_DATA.l_kl10f = [];
                LOTTERYLIST_DATA.l_k3 = [];
                LOTTERYLIST_DATA.l_dpc = [];
                // LOTTERYLIST_DATA.l_keno = [];
                // LOTTERYLIST_DATA.l_lucky = [];
                // 已登录
                $.each(data.list, function (key, val) {
                    if ($.inArray(val.shortName, STATIC_DATA.l_ssc) > -1) {
                        LOTTERYLIST_DATA.l_ssc[$.inArray(val.shortName, STATIC_DATA.l_ssc)] = val;
                    } else if ($.inArray(val.shortName, STATIC_DATA.l_11x5) > -1) {
                        LOTTERYLIST_DATA.l_11x5[$.inArray(val.shortName, STATIC_DATA.l_11x5)] = val;
                    } else if ($.inArray(val.shortName, STATIC_DATA.l_kl10f) > -1) {
                        LOTTERYLIST_DATA.l_kl10f[$.inArray(val.shortName, STATIC_DATA.l_kl10f)] = val;
                    } else if ($.inArray(val.shortName, STATIC_DATA.l_k3) > -1) {
                        LOTTERYLIST_DATA.l_k3[$.inArray(val.shortName, STATIC_DATA.l_k3)] = val;
                    }else if ($.inArray(val.shortName, STATIC_DATA.l_dpc) > -1) {
                        LOTTERYLIST_DATA.l_dpc[$.inArray(val.shortName, STATIC_DATA.l_dpc)] = val;
                    }
                    // else if ($.inArray(val.shortName, STATIC_DATA.l_keno) > -1) {
                    //     LOTTERYLIST_DATA.l_keno.push(val);
                    // } else if ($.inArray(val.shortName, STATIC_DATA.l_lucky) > -1) {
                    //     LOTTERYLIST_DATA.l_lucky.push(val);
                    // }
                    else if ($.inArray(val.shortName, STATIC_DATA.l_g_ssc) > -1) {
                        LOTTERYLIST_DATA.l_g_ssc[$.inArray(val.shortName, STATIC_DATA.l_g_ssc)]  = val;
                    }else if ($.inArray(val.shortName, STATIC_DATA.l_g_11x5) > -1) {
                        LOTTERYLIST_DATA.l_g_11x5[$.inArray(val.shortName, STATIC_DATA.l_g_11x5)] =val;
                    }else if ($.inArray(val.shortName, STATIC_DATA.l_g_k3) > -1) {
                        LOTTERYLIST_DATA.l_g_k3[$.inArray(val.shortName, STATIC_DATA.l_g_k3)] = val;
                    }else if ($.inArray(val.shortName, STATIC_DATA.l_g_xy3d) > -1) {
                        LOTTERYLIST_DATA.l_g_xy3d[$.inArray(val.shortName, STATIC_DATA.l_g_xy3d)] = val;
                    }else if ($.inArray(val.shortName, STATIC_DATA.l_g_keno) > -1) {
                        LOTTERYLIST_DATA.l_g_keno[$.inArray(val.shortName, STATIC_DATA.l_g_keno)] = val;
                    }else if ($.inArray(val.shortName, STATIC_DATA.l_g_lucky) > -1) {
                        LOTTERYLIST_DATA.l_g_lucky[$.inArray(val.shortName, STATIC_DATA.l_g_lucky)] = val;
                    }
                });
                appendDom(gameMenusDiv,LOTTERYLIST_DATA,gameMenuDiv);
            } else {
                gameMenusDiv.find("li").each(function(){
                    if(!$(this).attr("showname")) {
                        $(this).remove();
                    }
                });
            }
        },
        error: function() {
            // 当查询失败时，删除彩种列表里的所有彩种
            gameMenusDiv.find("li").each(function(){
                if($(this).attr("showname")) {
                    $(this).remove();
                }
            });
        },
        complete: function() {
            gameMenusDiv.find("dd").each(function(){
                if($(this).find("li").length < 1) {
                    $(this).remove();
                }
            });
        }
    });
}




function appendDom(dom,data,dom2){
    var html = '<dl>';
    $.each(data,function (key,val) {
        if(key.indexOf('keno')>-1){
            html+='<dd class=""><div class="qita ef-left">'+STATIC_DATA.dic_code_name[key]+'</div><ul>'
            html+='<li  onclick="openMenuUrl(\'game-klc.html\',true);">快乐8</li>'
            for(var i =0;i<val.length;i++){
                if(val[i].state !=0){
                    if($("#" + val[i].shortName).length >0) {
                        if($("." + val[i].shortName).hasClass("cur")) {
                            $("#" + val[i].shortName).show();
                        }
                        $("." + val[i].shortName).show();
                        loadOpenTimes(val[i].shortName);
                        openCodeList(val[i].shortName);
                    }
                }else{
                    dom2.find("li[showname="+val.shortName+"]").remove();
                }
            }
        }else if(key.indexOf('lucky')>-1){
            html+='<dd class=""><div class="qita ef-left">'+STATIC_DATA.dic_code_name[key]+'</div><ul>'
            html+='<li onclick="openMenuUrl(\'game-xy28.html\',true);">幸运28</li>'
        }else{
            if(key == 'l_g_ssc' || key == 'l_g_11x5'){
                html+='<dd class=""><div style="color: red" class="qita ef-left">'+STATIC_DATA.dic_code_name[key]+'</div><ul>'
            }else{
                html+='<dd class=""><div class="qita ef-left">'+STATIC_DATA.dic_code_name[key]+'</div><ul>'
            }

            for(var i =0;i<val.length;i++){
                if(val[i].state !=0){
                    var lottery_data = STATIC_DATA.lottery_data[val[i].shortName]?STATIC_DATA.lottery_data[val[i].shortName]:{};
                    if(lottery_data.Hot){
                        if(STATIC_DATA.lottery_data[val[i].shortName].Hot == '1'){
                            html+='<li style="color: red"  showname="'+val[i].shortName+'" onclick="javascript:transferLottery(\''+val[i].shortName+'\');">'+val[i].showName+'<i>H</i></li>'
                        }else{
                            html+='<li showname="'+val[i].shortName+'" onclick="javascript:transferLottery(\''+val[i].shortName+'\');">'+val[i].showName+'</li>'
                        }

                    }else{
                        html+='<li showname="'+val[i].shortName+'" onclick="javascript:transferLottery(\''+val[i].shortName+'\');">'+val[i].showName+'</li>'
                    }

                }

            }
        }
        html+='</ul></dd>'

    })

    html += '</dl>';
    dom.html(html)
}
